package cn.akwangl.spring.dependency.inject;

import cn.akwangl.spring.dependency.inject.annotation.UserGroup;
import cn.akwangl.spring.ioc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Collection;

/**
 * {@link Qualifier} 使用demo
 * <p>
 * {@link Qualifier} 具有逻辑分组的特性
 *
 * @author youyongkun
 * @date 2021/7/18 3:11 下午
 */
public class QualifierAnnotationDependencyInjectionDemo {
    /**
     * superUser -> primary =true
     */
    @Autowired
    private User user;

    /**
     * 指定 Bean 名称或 ID
     */
    @Autowired
    @Qualifier("user")
    private User namedUser;

    /**
     * 2 Beans = user + superUser
     */
    @Autowired
    private Collection<User> allUsers;

    /**
     * 2 Beans = user1 + user2 -> 4 Beans = user1 + user2 + user3 + user4
     */
    @Autowired
    @Qualifier
    private Collection<User> qualifiedUsers;

    /**
     * 2 Beans = user3 + user4
     */
    @Autowired
    @UserGroup
    private Collection<User> groupedUsers;

    /**
     * 进行逻辑分组
     */
    @Bean
    @Qualifier
    public User user1() {
        return createUser("7");
    }

    /**
     * 进行逻辑分组
     */
    @Bean
    @Qualifier
    public static User user2() {
        return createUser("8");
    }


    @Bean
    @UserGroup
    public static User user3() {
        return createUser("9");
    }

    @Bean
    @UserGroup
    public static User user4() {
        return createUser("10");
    }

    private static User createUser(String id) {
        User user = new User();
        user.setId(id);
        return user;
    }

    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class（配置类） -> Spring Bean
        applicationContext.register(QualifierAnnotationDependencyInjectionDemo.class);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        // 加载 XML 资源，解析并且生成 BeanDefinition
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        // 依赖查找 QualifierAnnotationDependencyInjectionDemo Bean
        QualifierAnnotationDependencyInjectionDemo demo = applicationContext.getBean(QualifierAnnotationDependencyInjectionDemo.class);

        // 期待输出 superUser Bean
        System.out.println("demo.user = " + demo.user);
        // 期待输出 user Bean
        System.out.println("demo.namedUser = " + demo.namedUser);
        // 期待输出 superUser user user1 user2
        System.out.println("demo.allUsers = " + demo.allUsers);
        // 期待输出 user1 user2
        System.out.println("demo.qualifiedUsers = " + demo.qualifiedUsers);
        // 期待输出 user3 user4
        System.out.println("demo.groupedUsers = " + demo.groupedUsers);

        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }

}
